//
//  LZGroupListTableViewCell.m
//  laziz_Merchant
//
//  Created by ZhaoDongBo on 2016/12/24.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZGroupListTableViewCell.h"

#import "Masonry.h"
#import "BlocksKit+UIKit.h"

#import "LZGroupItemModel.h"

@interface LZGroupListTableViewCell()

@property (nonatomic, strong) UILabel * nameLabel;
@property (nonatomic, strong) UILabel * priceLabel;
@property (nonatomic, strong) UIButton * editButton;
@property (nonatomic, strong) UIButton * deleteButton;
@property (nonatomic, strong) UIView * line;

@end

@implementation LZGroupListTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor = UIColorFromRGB(0xffffff, 1);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self configView];
        [self addAutoLayoutToCell];
    }
    return self;
}

- (void)configView {
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.priceLabel];
    [self.contentView addSubview:self.editButton];
    [self.contentView addSubview:self.deleteButton];
    [self.contentView addSubview:self.line];
}

- (void)addAutoLayoutToCell {
    
    WS(weakSelf);
    
    [self.nameLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.centerY);
        make.leading.mas_equalTo(20);
        make.width.mas_lessThanOrEqualTo(150);
    }];
    [self.priceLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.centerY);
        make.leading.mas_equalTo(weakSelf.nameLabel.mas_trailing).with.offset(15);
        make.trailing.mas_equalTo(weakSelf.editButton.mas_leading).with.offset(-15);
        make.width.mas_lessThanOrEqualTo(100);
    }];
    [self.editButton makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.centerY);
        make.trailing.mas_equalTo(weakSelf.deleteButton.mas_leading).with.offset(-15);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    [self.deleteButton makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.centerY);
        make.trailing.mas_equalTo(-20);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    [self.line makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
        make.leading.mas_equalTo(20);
        make.trailing.mas_equalTo(-20);
        make.height.mas_equalTo(0.6);
    }];
}

- (UILabel *)nameLabel {

    if (_nameLabel == nil) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.textColor = UIColorFromRGB(0x666666, 1);
        _nameLabel.font = [UIFont systemFontOfSize:15];
    }
    
    return _nameLabel;
}

- (UILabel *)priceLabel {
    
    if (_priceLabel == nil) {
        _priceLabel = [[UILabel alloc] init];
        _priceLabel.textColor = UIColorFromRGB(0x666666, 1);
        _priceLabel.font = [UIFont systemFontOfSize:15];
    }
    
    return _priceLabel;
}

- (UIButton *)editButton {

    WS(weakSelf);
    if (_editButton == nil) {
        _editButton = [[UIButton alloc] init];
        [_editButton setImage:[UIImage imageNamed:@"lz_group_edit"] forState:UIControlStateNormal];
        [_editButton bk_addEventHandler:^(id  _Nonnull sender) {
            if (weakSelf.didEditButtonBlock) {
                weakSelf.didEditButtonBlock(sender);
            }
        } forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _editButton;
}

- (UIButton *)deleteButton {

    WS(weakSelf);
    if (_deleteButton == nil) {
        _deleteButton = [[UIButton alloc] init];
        [_deleteButton setImage:[UIImage imageNamed:@"lz_group_delete"] forState:UIControlStateNormal];
        [_deleteButton bk_addEventHandler:^(id  _Nonnull sender) {
            if (weakSelf.didDeleteButtonBlock) {
                weakSelf.didDeleteButtonBlock(sender);
            }
        } forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _deleteButton;
}

- (UIView *)line {
    
    if (_line == nil) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = UIColorFromRGB(0xeeeeee, 1);
    }
    
    return _line;
}

- (void)setModel:(LZGroupItemModel *)model {
    _model = model;
    self.nameLabel.text = model.foodName;
    self.priceLabel.text = [NSString stringWithFormat:@"%0.3fBHD", model.foodPrice];
}

@end
